#include "UnlockDevice.h"
#include "db/DBManager.h"

#include <QJsonObject>
#include <QImage>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QJsonObject>
#include <QSqlError>
#include <QJsonArray>
#include <QDir>
#include <QFileInfoList>
#include <QDebug>
#include <QCoreApplication>

UnlockDevice* UnlockDevice::m_instance=NULL;

UnlockDevice::UnlockDevice()
{
    m_instance = this;
}

UnlockDevice *UnlockDevice::instance()
{
    return m_instance;
}

void UnlockDevice::handleSession()
{
    QMap<QString, QString> mapQuery = m_session->requestUrlQuery();
    QString strDeviceName = mapQuery.value("device_name");
    QString strWorkerPhone = mapQuery.value("worker_phone");
    QString strProblems = mapQuery.value("problems");

    // if there is no problem, unlock
    if ("0" == strProblems)
    {
        QString strCmd = QString("unlockDevice:%1").arg(strDeviceName);
    //    qDebug() << "strCmd: " << strCmd;
    //    qDebug() << "writeLocalSocket: " << s_localSocket->write(strCmd.toUtf8());
    //    m_session->replyFile(QString("D:/MONEY/BP/1.jpg" ));
        emit writeCmd(strCmd);

    }

    // there are problems
    else
    {

    }

    QString strSql = QString("insert into device_request(szDeviceName,szWorkerPhone,szProblems) "
                             "values('%1','%2','%3');");

    strSql = strSql.arg(strDeviceName).arg(strWorkerPhone).arg(strProblems);


    QSqlDatabase db = DBManager::database("core_config");
    QSqlQuery query(db);

    if(!query.exec(strSql))
    {
        qDebug()<<"insert into device_request error, "<<query.lastError().text();
        m_session->replyText(QString("ng"));
        return;
    }

    strSql = "select last_insert_id()";
    if (!query.exec(strSql))
    {
        qDebug()<<"select last_insert_id() error, "<<query.lastError().text();
        m_session->replyText(QString("ng"));
        return;
    }

    QString strLastInsertId = "";
    if (query.next())
    {
        strLastInsertId = query.value(0).toString();
    }

    qDebug() << "strLastInsertId: " << strLastInsertId;


    QString strImgDir = "pics/"+strDeviceName;
    QDir dir(strImgDir);
    QFileInfoList listFileInfo = dir.entryInfoList(QStringList()<<"*.jpg",
                                                   QDir::Files,
                                                   QDir::Name);


    QString strDstFolder = QCoreApplication::applicationDirPath()+"/PICS_STORE/"+strLastInsertId;
    qDebug() << "mkpath: " << dir.mkpath(strDstFolder);

    foreach (QFileInfo finfo, listFileInfo) {
        QString strFileName = finfo.fileName();
        QFile::copy(finfo.absoluteFilePath(), strDstFolder+"/"+strFileName);
    }

    // delete the processed unlock request
    for (int i=0; i<s_listRequstUnlock.size(); ++i)
    {
        QString strDeviceNameTmp = s_listRequstUnlock.at(i).value("device_name");

        if (strDeviceName == strDeviceNameTmp)
        {
            s_listRequstUnlock.removeAt(i);
            break;
        }
    }

    m_session->replyText(QString("ok"));
}
